package com.matt.controller.web.photo;

import com.matt.commons.dto.sso.AreaDTO;
import com.matt.commons.model.StatusResponse;
import com.matt.commons.page.PagerQuery;
import com.matt.domain.photoalbum.PhotoAlbumStatic;
import com.matt.foundation.model.MemVerificationCache;
import com.matt.service.Photo.PhotoAlbumStaticPageService;
import com.matt.service.remoteCall.SsoCallService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.matt.foundation.constant.RequestConstant.TICKET;

@RestController
@RequestMapping("/web/static_photo_album")
@Slf4j
public class WebPhotoAlbumStaticPageController {

    @Autowired
    private PhotoAlbumStaticPageService photoAlbumService;

    @Autowired
    private MemVerificationCache cache;

    @Autowired
    private SsoCallService ssoCallService;

    @RequestMapping("/albums")
    public StatusResponse childrenAlbumPage(@RequestHeader(TICKET) String ticket, String childrenId, PagerQuery pagerQuery ){
        String areaId = cache.getStaff(ticket).getAreaId();
        Page<PhotoAlbumStatic> page = photoAlbumService.childrenAlbumPage(childrenId, areaId, pagerQuery);
        return StatusResponse.success(page);
    }

    @RequestMapping("/detail")
    public StatusResponse albumDetail(@RequestHeader(TICKET) String ticket, String albumId ){
        PhotoAlbumStatic album = photoAlbumService.photoAlbumById(albumId);
        return StatusResponse.success(album);
    }

    @RequestMapping("/create")
    public StatusResponse createAlbum(@RequestHeader(TICKET) String ticket, String childrenId, String jsonStr){
        AreaDTO areaDTO = ssoCallService.getAreaDTO(ticket);
        String areaId = areaDTO.getId();
        PhotoAlbumStatic album = photoAlbumService.createPhotoAlbum(childrenId, areaId, areaDTO.getName(), jsonStr);
        return StatusResponse.success(album);
    }

    @RequestMapping("/all_valid_photos")
    public StatusResponse addLiveRecord(@RequestHeader(TICKET) String ticket, String childrenId, PagerQuery pagerQuery ){
        String areaId = cache.getStaff(ticket).getAreaId();
        Page<String> phtotos = photoAlbumService.getAllValidPhotoURL(childrenId, areaId, pagerQuery);
        return StatusResponse.success(phtotos);
    }

    @RequestMapping("/album_templates")
    public StatusResponse getAlbumTemplates(@RequestHeader(TICKET) String ticket, PagerQuery pagerQuery ){
        String areaId = cache.getStaff(ticket).getAreaId();
        List templates = photoAlbumService.getAllTemplates(areaId);
        return StatusResponse.success(templates);
    }

    @RequestMapping("/album_template_pages")
    public StatusResponse getAlbumTemplatePages(@RequestHeader(TICKET) String ticket, String templateId ){
        String areaId = cache.getStaff(ticket).getAreaId();
        List pages = photoAlbumService.getAllTemplatePages(templateId);
        return StatusResponse.success(pages);
    }
}
